// 实现这个项目的构建任务
const { src, dest, series, parallel, watch } = require('gulp')

// 为了使代码更加简洁，此处我们使用gulp的自动引用插件
const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()

// 每一次的编译为了避免不必要的问题，我们需要将编译文件目录清空
const del = require('del')

// 开发服务器
const browserSync = require('browser-sync')
const bs = browserSync.create()

// 数据
const data = {
  menus : [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

// 创建样式文件的读取、转换、写入
const styles = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: 'expanded' }))
    .pipe(dest('temp'))
}

// 创建script文件的读取、转换、写入
const scripts = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
}

// 创建模版文件的读取、转换、写入
const pages = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data }))
    .pipe(dest('temp'))
}

// 其他文件的复制、写入
const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('dist'))
}

// 图片文件的压缩、写入
const images = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}
// 字体文件的压缩、写入
const fonts = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// 清除编译文件目录
const clean = () => {
  return del(['dist', 'temp'])
}

// 启用开发阶段使用的本地任务
const bsServe = () => {
  watch('src/assets/styles/*.scss', styles)
  watch('src/assets/scripts/*.js', scripts)
  watch('src/*.html', pages)
  watch(['src/assets/images/**', 'src/assets/fonts/**', 'pbulic/**'], bs.reload)

  bs.init({
    files: 'dist/**',
    notify: false,
    server: {
      baseDir: ['temp', 'src', 'publish'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

const useref = () => {
  return src('temp/*.html', { base: 'temp' })
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true,
      removeComments: true
    })))
    .pipe(dest('dist'))
}

// 并行组合需要编译的任务
const compile = parallel(styles, pages, scripts)

// 组合部署阶段的打包操作任务
const build = series(clean, parallel(series(compile, useref), images, fonts, extra))

// 组合先编译在启用开发服务的任务
const serve = series(compile, bsServe)

module.exports = {
  clean,
  serve,
  build,
}